'use strict'

import { app, protocol, BrowserWindow, globalShortcut } from 'electron'
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
const ipc = require('electron').ipcMain
const path = require('path')
const { createListen, closeListen, createHttpServer } = require('@/utils/socket.js')
// import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
const isDevelopment = process.env.NODE_ENV !== 'production'

// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
  { scheme: 'app', privileges: { secure: true, standard: true }}
])

// Lock
if (!app.requestSingleInstanceLock()) {
  app.quit()
}

async function createWindow() {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 500,
    height: 200,
    autoHideMenuBar: true,
    resizable: false, // 是否支持修改大小
    maximizable: false, // 是否支持最大化
    skipTaskbar: false, // 不显示任务栏是否
    icon: path.join(__dirname, './assets/logo.ico'),
    webPreferences: {
      nodeIntegration: true, // 是否集成 Nodejs
      contextIsolation: false, // 上下文隔离
      enableRemoteModule: true, // 启用远程模块
      webSecurity: false, // 网络安全
      preload: path.join(__dirname, 'preload.js')
    }
  })

  global['main-window'] = mainWindow
  if (process.env.WEBPACK_DEV_SERVER_URL) {
    // Load the url of the dev server if in development mode
    await mainWindow.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
    // if (process.env.NODE_ENV !== 'production') mainWindow.webContents.openDevTools() // 开始调试模式
  } else {
    createProtocol('app')
    // Load the index.html when not in development
    mainWindow.loadURL('app://./index.html')
  }

  if (process.platform === 'win32') {
    const WM_INITMENU = 0x116
    mainWindow.hookWindowMessage(WM_INITMENU, e => {
      mainWindow.setEnabled(false)
      setTimeout(() => {
        mainWindow.setEnabled(true)
      }, 100)
      return true
    })
  }
}

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

// 快捷键注册
function registryShortcut() {
  globalShortcut.register('CommandOrControl+J+K', () => {
    // 获取当前窗口
    global['main-window'].webContents.openDevTools()
  })
}
  app.disableHardwareAcceleration() // 禁止gpu加速
app.whenReady().then(() => {
  createWindow()

  app.on('activate', function() {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })

  // 注册快捷键
  registryShortcut()
})

// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {
  if (process.platform === 'win32') {
    process.on('message', (data) => {
      if (data === 'graceful-exit') {
        app.quit()
      }
    })
  } else {
    process.on('SIGTERM', () => {
      app.quit()
    })
  }
}
// 开启监听
ipc.on('handlelisten', (event, data) => {
  if (data.isOpen === true) {
    createListen(data.port)
  } else {
    closeListen()
  }
})

// 创建socket
createHttpServer(3000)
